﻿using System.IO;
using System.Linq;
using System.Web;
using CoreLibrary;
using CoreLibrary.Storage;
using Entities.DataBase;
using WebService.Security;

namespace WebService.Web {
  
    public class FileUploadControl : AbstractControl {

        protected override void Process(UserToken user, HttpRequest request, HttpResponse response) {
            string[] allowedExtensions = { "jpg", "jpeg", "png" };
            var postedFile = request.Files["file"];
            if (postedFile == null) {
                return;
            }

            var name = postedFile.FileName;
            var fileName = Path.GetFileNameWithoutExtension(name);
            var fileExt  = Path.GetExtension(name);
            if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(fileExt)) {
                return;
            }

            if (!allowedExtensions.Any(fileExt.Equals)) {
                 return;
            }

            var isFront = true;
            var isBack  = true;

            Services.Locate<AmazonS3StorageService>().PutItem(new StorageItem());
            
        }

        public static string CreatePath(string name) {
            return StringUtil.Join("/", new[] {
                "Upload",
                "products",
                "images",
                name
            });
        }

        public static string UniqueFilename(string fileName, string fileExt, string productId) {
            var filename = fileName;
            return filename + "_" + productId + "." + fileExt;
        }

        
    }
}
